<?php
/*************************************************************************
Class Pick_Dao_CustomerCategory
----------------------------------------------------------------
Derasoft CMS Project
Company: Pick Co., Ltd                                  
Email: admin@pick.vn                                    
Last updated: 13/09/2011
**************************************************************************/
class Pick_Dao_CustomerCategory extends Pick_Core_Objects {
	function Pick_Dao_CustomerCategory($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->table = DB_PREFIX."customer_category";	
	}		
	/*-----------------------------------------------------------------------*
	* Function: getObjects
	* Parameter: WHERE condition
	* Return: Array of Info objects
	*-----------------------------------------------------------------------*/
	function getObjects($page = 1, $condition = '`cid` = 0', $sort = array(), $items_per_page = 0) {
		if(!$page) $page = 1;
		$start = ($page -1) * $items_per_page;
		$results = $this->select('*', $condition, $sort, $start, $items_per_page);
		if($results) {
			$objectInfos = array();
			foreach($results as $key => $result) {
				$objectInfos[] = new CustomerCategoryInfo ($result);
			}
			return $objectInfos;
		}
		return '';
	}
	/*-----------------------------------------------------------------------*
	* Function: getSubCategory
	* Parameter: WHERE condition
	* Return: array Object
	*-----------------------------------------------------------------------*/	
	function getSubCategory($cid) {
		return $this->getObjects(1, "`cid` = '$cid' AND status = 1", array('position'=>'ASC'));
	}		
	######### get chuyen muc con ###########
	function getSubCategory($cid) {
		return $this->getObjects(1, "status =1 AND `cid` = '$cid' AND status = 1",array('position'=>'ASC'),0,'');
	}	
	/*-----------------------------------------------------------------------*
	* Function: getSubCategoryAll
	* Parameter: WHERE condition
	* Return: array Object
	*-----------------------------------------------------------------------*/	
	function getSubCategoryAll($pId) {
		$results = $this->getObjects(1, "`cid` = '$pId' ", array('position'=>'ASC'));
		if(is_array($results)) {
			return $results;
		}
		return '';
	}
	/*-----------------------------------------------------------------------*
	* Function: optionAllCategories
	* Parameter: Info object
	* Return: a string 
	*-----------------------------------------------------------------------*/		
	function optionAllCategories($cid = 0, $value = '', $lang = 'vn', $sort = array('position' => 'ASC'), $level = 0){
		$str = '&nbsp;';		
		$objectCategory = $this->getSubCategory($cid);
		if(count($objectCategory)>0):
			$pace = '';
			for($i=$level; $i>0; --$i):
				$pace .= '&nbsp;&nbsp;&nbsp;'; 
			endfor;	
			++$level;			
			foreach($objectCategory as $object):		
				$str .= "<option value='".$object->getField('id')."'".($object->getField('id')==$value?" selected=\"selected\"":"").">" . $pace . $object->getFieldLang('name'). "</option>";				
				$str .= $this->optionAllCategories($object->getField('id'), $value, $lang, $sort, $level);
			endforeach;
		endif;	
		return $str;
	}
	/*-----------------------------------------------------------------------*
	* Function: getParentName
	* Parameter: Info object
	* Return: a string 
	*-----------------------------------------------------------------------*/		
	function getParentName($cid, $lang = 'vn') {
		$object = $this->getObject($cid);
		if($object) {
			return $object->getFieldLang('name');
		}
		return "/";
	}		
	/*-----------------------------------------------------------------------*
	* Function: getTreeCategory
	* Parameter: Info object
	* Return: a tree object category
	*-----------------------------------------------------------------------*/
	function getTreeCategory($pid = 0, $sort = array('position' => 'ASC')){

		$objectTree = array();
		$objectCategory = $this->getSubCategory($pid);
		if(count($objectCategory)>0):
			foreach($objectCategory as $object):
				$array = array();
				$array[0] = $object;
				$array[1] = $this->getTreeCategory($object->getId(), $sort);
				if(count($array[1])):
					$objectTree[] = $array;	
				else:
					$objectTree[] = array($array[0]);	
				endif;
				
			endforeach;
		endif;
		return $objectTree;

	}
	###### Lay id cua catagory con dua vao mot mang ######
	###### Neu mà $flag == 1 thì lay luon $pId ######
	function getIdSubCategory($pId, $flag = 1) {
		$objectCategorys = $this->getObjects(1, "status =1 AND `cid` = '$pId'",array('position'=>'ASC'), '');
		if(count($objectCategorys)>0):
			$arrayId = array();
			if($flag == 1) $arrayId[] = $pId;
			foreach($objectCategorys as $object):
				$arrayId[] = $object->getId();
				$havearray = $this->getIdSubCategory($object->getId(), 0);
				if(count($havearray)>0)
					foreach($havearray as $id):
						$arrayId[] = $id;
					endforeach;
			endforeach;
			return $arrayId;
		endif;
		return array($pId);
	}	
	/*	
	# Lay id cua catagory con dua vao mot mang. Function này có hiệu lực với tất cả các 
	# status.
	# Neu mà $flág == 1 thì lay luon $cid
	*/
	function getIdSubCategoryALL($cid, $flag = 1) {
		$objectCategorys = $this->getObjects(1, "`cid` = '$cid'",array('position'=>'ASC'), '');
		if(count($objectCategorys)>0):
			$arrayId = array();
			if($flag == 1) $arrayId[] = $cid;
			foreach($objectCategorys as $object):
				$arrayId[] = $object->getField('id');
				$havearray = $this->getIdSubCategory($object->getField('id'), 0);
				if(count($havearray)>0)
					foreach($havearray as $id):
						$arrayId[] = $id;
					endforeach;
			endforeach;
			return $arrayId;
		endif;
		return array($cid);
	}
	
	function getLevelCategory($id){
		$objects = $this->getSubCategory($id);
		$array = array();
		foreach($objects as $object):
			$array_1 = array();
			$objects_1 = $this->getSubCategory($object->getId());
			$array_1[0] = $object;
			$array_1[1] = $objects_1;
			$array[]=$array_1;
		endforeach;
		return $array;
	}
/*-----------------------------------------------------------------------*
* Function: Lấy đường dẫn đi từ đối tượng con --> đối tượng ông nội.
* Parameter: WHERE catproduct
* Return:array ID
*-----------------------------------------------------------------------*/		
	function getPathFromSlug($slug, $lang = 'vn', $module = 'san-pham'){
		global $base_url;
		$stringpath = '';
		$object = $this->getObjectFromSlug($slug);
		$stringpath .= is_object($object)?"<a href='".$base_url."/".$lang."/".$module."/".$object->getSlug().".html'>" . $object->getName($lang) . "</a>":$stringpath;
		$objectCategory = $this->getObjectCategoryFromSlug($slug);
		if(is_object($objectCategory)){
			$stringpath = "<a href='".$base_url."/".$lang."/".$module."/".$objectCategory->getSlug().".html'>" . $this->getPathFromSlug($objectCategory->getSlug(), $lang) . "</a>" . " &raquo; " . $stringpath;
		}
		return $stringpath;
	}
/*-----------------------------------------------------------------------*
* Function: cleanTrash xóa tất cả chuyên mục và mẫu tin trong chuyên mục co trang thai la 2
* Parameter: WHERE catproduct
* Return:array ID
*-----------------------------------------------------------------------*/	
	function cleanTrash($condition = 'status = 2'){
		$dem = 0;
		$objectDelete = $this->getObjects(1, $condition, '', '');
		foreach($objectDelete as $object):
			$arrayIdCategory = $this->getIdSubCategory($object->getField('id'),1);
			$product = new Product;
			$arrayObject = $product->getObjectsFromArrayCId(1,$arrayIdCategory);
			foreach($arrayObject as $objectImages):
				$ok1 = $objectImages->deleteAvatar();
				$ok2 = $product->delete("id = " . $objectImages->getField('id'));
				if(!$ok1 && !$ok2) $dem++;				
			endforeach;
			$dem = $object->deleteAvatar()?$dem:$dem+1;
			$dem = $this->delete('id IN(' . implode(",", $arrayIdCategory).')')?$dem:$dem+1;
		endforeach;
		return $dem==0?true:false;
	}	
}

?>